#![allow(dead_code)]
use std::time::Duration;
use crate::config::const_store::global_store;
use crate::service::syn_zh_service::{syn_zh_with_time_update};
use crate::utils::app::get_time;

use crate::utils::http_auth_manage::get_token;
use crate::utils::TaskManager;

pub fn execute(){
    log::info!("execute_service start");
    TaskManager::global().spawn_periodic(Duration::from_secs(10), || async {
        log::info!("【周期任务】执行中...");
        let token_result = get_token().await;
        if token_result.is_err() {
            log::error!("【周期任务】获取 token 失败，跳过本次任务");
            return Ok(());
        }

        TaskManager::global().spawn_once_task("同步配置", syn_config_task);
        TaskManager::global().spawn_once_task("同步货架信息", syn_shelf_task);

        Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
    });

    log::info!("execute_service end");
}

///同步配置任务
pub async fn syn_config_task()-> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
    log::info!("【周期任务】同步配置任务开始...");
    Ok(false)
}

///同步货架信息
pub async fn syn_shelf_task()-> Result<bool, Box<dyn std::error::Error + Send + Sync>> {
    let zh_cfg =global_store().read().syn_zhcfg.clone();
    let now=get_time();
    let flag=now-zh_cfg.syn_zh_time> zh_cfg.syn_zh_interval;
    if !flag{
        return Ok(false)
    }
    let _ = syn_zh_with_time_update(0).await?;
    Ok(true)
}

